<?php
require('includes/top.php');

$laptop_id = $_POST['laptop_id'];
//echo $laptop_id;
$remarks = $_POST['laptopremarks'];
$model_no = $_POST['laptop_model'];
$serial_no = $_POST['serial_no'];
$dispatch_location = $_POST['dispatch_location'];
$user_id = $_POST['user'];
$type = $_POST['type'];

$date = date("d-m-Y");

$user_address=$_SESSION['user_address'];


if($_POST['type']=="Return")
{
  $sql="INSERT INTO return_laptop(`laptop_id`,`remarks`,`model_no`,`serial_no`,`dispatch_location`,`date`,`status`,`user_id`) VALUES ('$laptop_id','$remarks','$model_no','$serial_no','$dispatch_location','$date','$type','$user_id')";
$result=mysql_query($sql);
$sql4="UPDATE laptop SET `returned` = '1' WHERE laptop_id = '".$laptop_id."'";
	$result1=mysql_query($sql4);
} 
elseif($_POST['type']=="TakenHome")
{
   
   $sql="INSERT INTO return_laptop(`laptop_id`,`remarks`,`model_no`,`serial_no`,`dispatch_location`,`date`,`status`,`user_id`) VALUES ('$laptop_id','$remarks','$model_no','$serial_no','$dispatch_location','$date','$type','$user_id')";
   $result=mysql_query($sql);
   $sql14="UPDATE laptop_details SET laptop_type='X' WHERE laptop_id = '".$laptop_id."'";
   $result14=mysql_query($sql14);
   
}
elseif($_POST['type']=="Lost")
{
  
 $sql="INSERT INTO return_laptop(`laptop_id`,`remarks`,`model_no`,`serial_no`,`dispatch_location`,`date`,`status`,`user_id`) VALUES ('$laptop_id','$remarks','$model_no','$serial_no','$dispatch_location','$date','$type','$user_id')";
   $result=mysql_query($sql);
   $sql15="UPDATE laptop_details SET laptop_type='L' WHERE laptop_id = '".$laptop_id."'";
   $result15=mysql_query($sql15);
   
}
else
{
  $sql="INSERT INTO return_laptop(`laptop_id`,`remarks`,`model_no`,`serial_no`,`dispatch_location`,`date`,`status`,`user_id`) VALUES ('$laptop_id','$remarks','$model_no','$serial_no','$dispatch_location','$date','$type','$user_id')";
   $result=mysql_query($sql);
   $sql18="UPDATE laptop_details SET laptop_type='D' WHERE laptop_id = '".$laptop_id."'";
   $result18=mysql_query($sql18);
   
}


if(isset($_POST["acc_id"]) && !empty($_POST["acc_id"])){	
					  $Acc_id=array_values($_POST["acc_id"]);
					  $Type_acc=array_values($_POST["type_acc"]);
					  $User_id=array_values($_POST["user_id"]);										
	 				  $count=sizeof($Acc_id);										  
					  for($i=0;$i<=$count;$i++){					  						  					  
					     if(trim($Acc_id[$i])!=''){
						    $AccId=addslashes(trim($Acc_id[$i]));
							$TypeAcc=addslashes(trim($Type_acc[$i]));
							$UserId=addslashes(trim($User_id[$i]));
																											
								if($TypeAcc=='Return')
									{
									//echo $laptop_id;
$sql1="INSERT INTO returned_laptops_acc(`accessories_id`,`laptop_id`,`date`,`status`,`user_id`) VALUES ('$AccId','$laptop_id','$date','$TypeAcc','$UserId')";
$result1=mysql_query($sql1);
$sql6="UPDATE laptop_acc_details SET `returned` = '1' WHERE accessories_id = '".$AccId."' and user_id = '".$UserId."'";
$result6=mysql_query($sql6);
							  }
								else
								{
								 $sql1="INSERT INTO returned_laptops_acc(`accessories_id`,`laptop_id`,`date`,`status`,`user_id`) VALUES      ('$AccId','$laptop_id','$date','$TypeAcc','$UserId')";
                                 $result1=mysql_query($sql1);
								}
									
																																						
																				
						 }										
					  
		}
					
	  }	 

if(isset($_POST["accessories_id"]) && !empty($_POST["accessories_id"])){	
					  $Accessories_id=array_values($_POST["accessories_id"]);
					  $Acc=array_values($_POST["acc"]);	
					  $Acc_remark=array_values($_POST["accessoriesremarks"]);										
					  $User_id=array_values($_POST["acc_user_id"]);										
	 				  $count=sizeof($Accessories_id);										  
					  for($i=0;$i<=$count;$i++){					  						  					  
					     if(trim($Accessories_id[$i])!=''){
						    $AccessoriesId=addslashes(trim($Accessories_id[$i]));
							$Acc1=addslashes(trim($Acc[$i]));
							$AccRemark=addslashes(trim($Acc_remark[$i]));														
							$UserId=addslashes(trim($User_id[$i]));	
						
																			
$sql3="INSERT INTO return_accessories(`accessories_id`,`remarks`,`laptop_id`,`date`,`status`,`user_id`) VALUES ('$AccessoriesId','$AccRemark','$laptop_id','$date','$Acc1','$UserId')";
$result3=mysql_query($sql3);
$sql7="UPDATE accessories SET `returned` = '1' WHERE user_id = '".$UserId."' and accessories_id = '".$AccessoriesId."'";
$result7=mysql_query($sql7); 
	 }										
					  
		}
					
	  }	 
	  
	  //added here for substitute laptops
	   $laptop_ids = $_POST['laptop_ids'];
	   $remarkss = $_POST['laptopremarkss'];
	   $model_nos = $_POST['laptop_models'];
	   $serial_nos = $_POST['serial_nos'];
	   $dispatch_locations = $_POST['dispatch_locations'];
	   
       $types = $_POST['typeslap'];
	   $date = date("d-m-Y");
	   //echo $type;
	     if($_POST['typeslap']=='Return')
		   {
		    $sqlsub="INSERT INTO substitute_laptop (`laptop_id`,`remarks`,`model_no`,`serial_no`,`dispatch_location`,`Date`,`Status`,`user_id`) VALUES ('  $laptop_ids','$remarkss','$model_nos','$serial_nos','$dispatch_locations','$date','$types','$user_id')";
			$result=mysql_query($sqlsub);
			$sqlupdate="Update laptop_detail SET status='1',laptop_type='S',depot_location='".$dispatch_locations."'where laptop_id='".trim($laptop_ids)."'";
			mysql_query($sqlupdate);
		   }
		 else
		   {
		       $sqlsub="INSERT INTO substitute_laptop (`laptop_id`,`remarks`,`model_no`,`serial_no`,`dispatch_location`,`Date`,`Status`,`user_id`) VALUES ('  $laptop_ids','$remarkss','$model_nos','$serial_nos','$dispatch_locations','$date','$types','$user_id')";
			mysql_query($sqlsub);
		   }
	  
	  


$modelname_array = explode('-',$_POST['laptop_model']);

$sql9="UPDATE laptop_details SET `depot_location` = '".$user_address."' WHERE laptop_id = '".trim($laptop_id)."'";
$result9=mysql_query($sql9);

	if($result>0)
	{
	header('Location:return_laptop.php?v=success');
	}
	else
	{
	header('Location:return_laptop.php?v=err');
    }

?>